﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ClassLibrary;
using Param = ClassLibrary.Parameters.Values;
using System.Resources;
using System.Collections.Generic;

namespace ControlLibrary
{
    public partial class EventList : UserControl
    {
        public delegate void OnNewEvent();
        public delegate void OnEditEvent(Guid guid);
        public delegate void OnDeleteEvent(Guid guid);

        [Category("Configuration"), Browsable(true), Description("")]
        public event OnNewEvent onNewEvent;

        [Category("Configuration"), Browsable(true), Description("")]
        public event OnEditEvent onEditEvent;

        [Category("Configuration"), Browsable(true), Description("")]
        public event OnDeleteEvent onDeleteEvent;

        public EventList()
        {
            InitializeComponent();
            addItemNewEvent();
        }

        public void clear()
        {
            flowLayoutPanel1.Controls.Clear();
            addItemNewEvent();
        }


        #region public

        public void UpdateUI()
        {
            List<EventItem> list;
            bool RC = getItems(out list);
            if (true == RC)
            {
                foreach (EventItem item in list)
                {
                    item.UpdateUI();
                }
            }
            setHighlightMaxWidth();
        }

        public bool addItem(Guid ID, string date, string titre, bool lien)
        {
            try
            {
                //New line          
                EventItem newEvent = new EventItem(ID, date, titre, lien);
                newEvent.Width =
                    flowLayoutPanel1.Width -
                    flowLayoutPanel1.Margin.Right -
                    flowLayoutPanel1.Margin.Left -
                    SystemInformation.VerticalScrollBarWidth;

                //Position
                int nbItem = flowLayoutPanel1.Controls.Count;
                flowLayoutPanel1.Controls.Add(newEvent);
                if (nbItem > 0)
                {
                    flowLayoutPanel1.Controls.SetChildIndex(newEvent, nbItem - 1);
                }

                //Event "selected"
                newEvent.onSelect += new EventItem.OnSelect(newEvent_onSelect);
                newEvent.onDelete += new EventItem.OnSelect(newEvent_onDelete);

                bool RC = setHighlightMaxWidth();
                if (!RC) throw new Exception("setHighlightMaxWidth failed");
                return true;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("EventList", "addItem", e, Param.RM.GetString("EL_Error_AddItem"));
                return false;
            }
        }

        public bool updateItem(Guid ID, string date, string titre, bool lien)
        {
            try
            {
                EventItem eventItem;
                bool RC = getItemFromID(ID, out eventItem);

                if (true == RC)
                {
                    eventItem.set(date, titre, lien);
                    //maj de la taille
                    eventItem.Width = flowLayoutPanel1.Width -
                                        flowLayoutPanel1.Margin.Right -
                                        flowLayoutPanel1.Margin.Left -
                                        SystemInformation.VerticalScrollBarWidth;
                    RC = setHighlightMaxWidth();
                    if (!RC) throw new Exception("setHighlightMaxWidth failed");    
                }
                else throw new Exception("getItemFromID failed");
                return true;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("EventList", "updateItem", e, Param.RM.GetString("EL_Error_UpdateItem"));
                return false;
            }
        }

        #endregion

        //Add "New Event" item
        private bool addItemNewEvent()
        {
            try
            {
                EventItem newEvent = new EventItem();
                flowLayoutPanel1.Controls.Add(newEvent);
                newEvent.Width =
                    flowLayoutPanel1.Width -
                    flowLayoutPanel1.Margin.Right -
                    flowLayoutPanel1.Margin.Left -
                    SystemInformation.VerticalScrollBarWidth;

                newEvent.onSelect += new EventItem.OnSelect(EventListItem_New_onSelect);

                bool RC = setHighlightMaxWidth();
                if (!RC) throw new Exception("setHighlightMaxWidth failed");
                return true;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("EventList", "addItemNewEvent", e);
                return false;
            }
        }

        private bool getMaxWidth(out int maxWidth)
        {
            maxWidth = 0;
            try
            {                
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    EventItem item = c as EventItem;
                    if (null != item)
                        maxWidth = Math.Max(maxWidth, item.ContentWidth);
                }
                return true;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("EventList", "getMaxWidth", e);
                return false;
            }
        }

        private bool setHighlightMaxWidth()
        {
            try
            {
                int maxWidth;
                bool RC = getMaxWidth(out maxWidth);

                if (true == RC)
                {
                    foreach (Control c in flowLayoutPanel1.Controls)
                    {
                        EventItem item = c as EventItem;
                        if (null != item)
                        {
                            item.highlightWidth = maxWidth;
                        }
                    }
                }
                else throw new Exception("getMaxWidth failed");
                return RC;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("EventList", "setHighlightMaxWidth", e);
                return false;
            }
        }

        private bool getItems(out List<EventItem> list)
        {
            list = new List<EventItem>();
            try
            {
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    EventItem item = c as EventItem;
                    if (null != item)
                        list.Add(item);
                    else 
                        throw new Exception("Item null");
                }
                return true;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("EventList", "getItems", e);
                return false;
            }
        }

        private bool getItemFromID(Guid ID, out EventItem Item)
        {
            Item = null;
            try
            {
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    EventItem item = c as EventItem;
                    if (null != item)
                    {
                        if (item.ID == ID)
                        {
                            Item = item;
                            break;
                        }
                    }
                }

                if (null == Item)
                    throw new Exception("Item not found");

                return true;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("EventList", "getItemFromID", e);
                return false;
            }
        }

        #region SEND EVENT

        //Select "New Item"
        private void EventListItem_New_onSelect(Guid ID)
        {
            if (null != onNewEvent)
                onNewEvent();
        }

        //Selection an item
        private void newEvent_onSelect(Guid ID)
        {
            if (null != onEditEvent)
                onEditEvent(ID);
        }

        //Delete an item
        private void newEvent_onDelete(Guid ID)
        {
            try
            {
                EventItem itemToDelete;
                bool RC = getItemFromID(ID, out itemToDelete);

                if (true == RC)
                {
                    if (null != itemToDelete && flowLayoutPanel1.Controls.Contains(itemToDelete))
                    {
                        flowLayoutPanel1.Controls.Remove(itemToDelete);
                        setHighlightMaxWidth();
                    }
                    else throw new Exception("unknown item to delete");

                    if (null != onDeleteEvent)
                        onDeleteEvent(ID);
                }
                else throw new Exception("getItemFromID failed");
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("EventList", "newEvent_onDelete", e, Param.RM.GetString("EL_Error_DelItem"));
            }
        }

        #endregion

        private void flowLayoutPanel1_SizeChanged(object sender, EventArgs e)
        {
            try
            {
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    c.Width =
                    flowLayoutPanel1.Width -
                    flowLayoutPanel1.Margin.Right -
                    flowLayoutPanel1.Margin.Left -
                    SystemInformation.VerticalScrollBarWidth;
                }

                flowLayoutPanel1.PerformLayout();
            }
            catch (Exception ex)
            {
                ErrorManager.WriteLog("EnventList", "flowLayoutPanel1_SizeChanged", ex);
            }
        }
    }
}
